package com.wangijun.yuan.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.core.domain.UserInfo;
import com.wangijun.yuan.web.inface.BaseCache;
import com.wangijun.yuan.web.mapper.UserInfoMapper;
import com.wangijun.yuan.web.model.dto.PersonalUserParam;
import com.wangijun.yuan.web.model.dto.QryUserParam;
import com.wangijun.yuan.web.model.dto.RankingUserQryParam;
import com.wangijun.yuan.web.model.vo.BaseUserVO;
import com.wangijun.yuan.web.model.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = BaseCache.webUser)
public class UserService extends ServiceImpl<UserInfoMapper, UserInfo> implements BaseCache<UserInfo> {

    private final OperateInfoService operateInfoService;

    @Override
    @Cacheable(key = "#id")
    public UserInfo get(Long id) {
        return this.getById(id);
    }

    // 获取基础用户信息
    public BaseUserVO getBaseUser(Long id) {
        UserInfo userInfo = SpringUtil.getBean(this.getClass()).get(id);
        return new BaseUserVO(
                userInfo.getId(),
                userInfo.getUsername(),
                userInfo.getAvatar(),
                userInfo.getSummary()
        );
    }


    @Override
    @CacheEvict(key = "#upd.id")
    public void changeById(UserInfo upd) {
        this.updateById(upd);
    }

    @Override
    @CacheEvict(key = "#id")
    public void cacheEvict(Long id) {
    }

    /**
     * 功能描述: 作者用户热度榜单
     *
     * @author wlf 2024/7/21 9:28
     */
    public CommonPage<UserInfoVO> rankingUser(RankingUserQryParam param) {
        LambdaQueryChainWrapper<UserInfo> wrapper = this.lambdaQuery();
        wrapper.orderByDesc(UserInfo::getReadNum);
        Page<UserInfo> page = wrapper.page(param.page());
        List<Long> favUserIds = operateInfoService.getFollowUserIds();
        return CommonPage.toPage(page, UserInfoVO.class, (vo) -> vo.setFollowFlag(favUserIds.contains(vo.getId())));
    }


    /**
     * 功能描述: 获取用户关注列表以及粉丝列表
     *
     * @author wlf 2024/7/11 14:46
     */
    public CommonPage<UserInfoVO> personalUserList(PersonalUserParam param) {
        List<Long> ids = List.of();
        switch (param.getType()) {
            case follow -> ids = operateInfoService.getFollowUserIds(param.getUserId());
            case fans -> ids = operateInfoService.getFansUserIds(param.getUserId());
        }
        if (ids.isEmpty()) {
            return CommonPage.toPage(param.page(), UserInfoVO.class);
        }
        Page<UserInfo> page = lambdaQuery()
                .in(UserInfo::getId, ids)
                .like(StrUtil.isNotBlank(param.getText()), UserInfo::getUsername, param.getText())
                .orderByDesc(UserInfo::getUpdateTime)
                .page(param.page());
        List<Long> favUserIds = operateInfoService.getFollowUserIds();
        return CommonPage.toPage(page, UserInfoVO.class, (u) -> u.setFollowFlag(favUserIds.contains(u.getId())));
    }

    public CommonPage<UserInfoVO> qryUser(QryUserParam param) {
        Page<UserInfo> page = lambdaQuery()
                .like(StrUtil.isNotBlank(param.getText()), UserInfo::getUsername, param.getText())
                .orderByDesc(UserInfo::getUpdateTime)
                .page(param.page());
        List<Long> favUserIds = operateInfoService.getFollowUserIds();
        return CommonPage.toPage(page, UserInfoVO.class, (u) -> u.setFollowFlag(favUserIds.contains(u.getId())));
    }
}
